<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="所属公司">
        <el-select v-model="gongsiSel" @change="initData" placeholder="所属公司">
          <el-option v-for="val in gongsiList" :key="val.id" :label="val.name" :value="val.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="handleAddRole">
      新增
    </el-button>

    <el-table :data="rolesList" style="width: 100%;margin-top:30px;" border>
      <!-- <el-table-column align="center" label="Role Key" width="220">
        <template slot-scope="scope">
          {{ scope.row.key }}
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="角色名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <!-- <el-table-column align="header-center" label="角色权限">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">
            编辑
          </el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑角色':'新增角色'">
      <el-form :model="role" label-width="120px" label-position="left">
        <el-form-item label="所属公司" v-if="dialogType==='new'">
          <el-select v-model="role.tenantId" placeholder="所属公司">
            <el-option v-for="val in gongsiList" :key="val.id" :label="val.name" :value="val.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色名称">
          <el-input v-model="role.name" placeholder="角色名称" />
        </el-form-item>
        <el-form-item label="角色权限设置">
          <!-- <el-tree ref="tree" :check-strictly="checkStrictly" :data="routesData" :props="defaultProps" @check-change="handleCheckChange" show-checkbox node-key="path" class="permission-tree" /> -->
          <el-select v-model="role.permissionIds" multiple placeholder="请设置">
            <el-option
              v-for="item in routesData"
              :key="item.id"
              :label="item.label"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">
          取消
        </el-button>
        <el-button type="primary" @click="confirmRole">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import path from 'path'
import { deepClone } from '@/utils'
import { getTenantIdRole, postRole, deleteRole, getRole, putRole } from '@/api/role'
import { permission } from '@/api/user'

const defaultRole = {
  "name": "",
  "tenantId": "",
  "permissionIds": []
}

export default {
  data() {
    return {
      role: Object.assign({}, defaultRole),
      routes: [],
      rolesList: [],
      dialogVisible: false,
      dialogType: 'new',
      checkStrictly: false,
      defaultProps: {
        children: 'children',
        label: 'label',
      },
      gongsiList:[],
      routesData:[],
      gongsiSel: '',
    }
  },
  computed: {},
  created() {
    this.gongsiList = JSON.parse(localStorage.getItem('companyList'));
    this.role.tenantId = this.gongsiList[0].id;
    this.gongsiSel = this.gongsiList[0].id;
    this.initData();
    this.permissionList();
  },
  methods: {
    //列表
    initData(){
      getTenantIdRole(this.gongsiSel).then((res)=>{
        this.rolesList = res.result;
      })
    }, 
    permissionList(){
      permission().then((res)=>{
        this.routesData = res.result;
      })
    },
    // handleCheckChange(data){
    //   this.role.permissionIds = [];
    //   for(let i=0;i<data.length;i++){
    //     this.role.permissionIds.push(data[i].id);
    //   }
    //   console.log(data)
    // },
    handleAddRole() {
      this.role = Object.assign({}, defaultRole)
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.dialogType = 'edit';
      getRole(scope.row.id).then((res)=>{
        let obj = res.result;
        this.role = Object.assign({}, defaultRole)
        delete this.role.tenantId;
        this.role.id = obj.id;
        this.role.name = obj.name;
        this.role.permissionIds = [];
        for(let i=0;i<obj.permissions.length;i++){
          this.role.permissionIds.push(obj.permissions[i].id)
        }
        this.dialogVisible = true
        // this.role.permissionIds = obj.permissions;
        console.log(this.role)
      })
    },
    handleDelete({ $index, row }) {
      this.$confirm('确认删除?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          deleteRole(row.id).then((res)=>{
            this.dialogVisible = false
            this.initData();
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        })
        .catch(err => { console.error(err) })
    },
    async confirmRole() {
      if(this.dialogType === 'edit'){
        putRole(this.role.id,this.role).then((res)=>{
          this.dialogVisible = false
            this.initData();
          this.$message({
            type: 'success',
            message: '编辑成功!'
          })
        })
      }else{
        postRole(this.role).then((res)=>{
          this.dialogVisible = false
            this.initData();
          this.$message({
            type: 'success',
            message: '创建成功!'
          })
        })
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
